/**
 * @author
 * @createtime
 * @class MaterialOutStockForm
 * @extends Ext.Window
 * @description MaterialOutStock表单
 */
MaterialOutStockForm = Ext.extend(Ext.Window, {
	//内嵌FormPanel
	formPanel : null,
	//构造函数
	constructor : function(_cfg) {
		Ext.applyIf(this, _cfg);
		//必须先初始化组件
		this.initUIComponents();
		MaterialOutStockForm.superclass.constructor.call(this, {
			id : 'MaterialOutStockFormWin',
			layout : 'fit',
			items : this.formPanel,
			modal : true,
			height : 300,
			width : 400,
			maximizable : true,
			title : '[物资出库]详细信息',
			buttonAlign : 'center',
			buttons : this.buttons
		});
	},//end of the constructor
	//初始化组件
	initUIComponents : function() {
		this.formPanel = new Ext.FormPanel({
			layout : 'form',
			trackResetOnLoad : true,
			bodyStyle : 'padding:10px 10px 10px 10px',
			border : false,
			url : __ctxPath + '/produce/saveMaterialOutStock.do',
			id : 'MaterialOutStockForm',
			defaults : {
				anchor : '98%,98%'
			},
			defaultType : 'textfield',
			items : [{
						name : 'materialOutStock.id',
						id : 'id',
						xtype : 'hidden',
						value : this.id == null ? '' : this.id
					},{
						name : 'materialOutStock.project.id',
						id : 'materialOutStock.project.id',
						xtype : 'hidden'
					}, {
						fieldLabel : '项目名称',
						name : 'materialOutStockProName',
						id : 'materialOutStockProName',
						maxLength : 64,
						allowBlank : false,
						emptyText : '请选择项目',
						listeners : {
							'focus' : function() {
								ProjectSelector.getView(function(proId, proName, proNo,contractAmount) {
									Ext.getCmp("materialOutStock.project.id").setValue(proId);
									Ext.getCmp("materialOutStockProName").setValue(proName);
								}, true).show();
							}
						}
					}, {
						fieldLabel : '出库日期',
						xtype:'datefield',
						format:'Y-m-d',
						allowBlank : false,
						format:'Y-m-d',
						name : 'materialOutStock.outStockDate',
						id : 'outStockDate'
					}, {
						fieldLabel : '出库单号',
						name : 'materialOutStock.outStockNo',
						id : 'outStockNo',
						allowBlank : false,
						maxLength : 64
					},{
						name : 'materialOutStock.material.id',
						id : 'materialOutStock.material.id',
						xtype : 'hidden'
					}, {
						fieldLabel : '物资名称',
						name : 'materialOutStockMaterialName',
						id : 'materialOutStockMaterialName',
						maxLength : 64,
						allowBlank : false,
						emptyText : '请选择物资',
						listeners : {
							'focus' : function() {
								MaterialSelector.getView(function(materialIds, materialNos, materialNames) {
									Ext.getCmp("materialOutStock.material.id").setValue(materialIds);
									Ext.getCmp("materialOutStockMaterialName").setValue(materialNames);
								}, true).show();
							}
						}
					}, {
						fieldLabel : '数量',
						name : 'materialOutStock.num',
						id : 'num',
						maxLength : 9,
						allowBlank : false,
						xtype:'numberfield'
					}, {
						fieldLabel : '单价(元)',
						name : 'materialOutStock.unitPrice',
						id : 'unitPrice',
						allowBlank : false,
						maxLength : 9,
						xtype:'numberfield',
						regex : /^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/,
						regexText : '请输入正确格式的金额'
					}, {
						fieldLabel : '备注',
						name : 'materialOutStock.remark',
						id : 'remark',
						xtype:'textarea'
					}

			]
		});
		//加载表单对应的数据	
		this.initData();
		//初始化功能按钮
		this.buttons = [{
					text : '保存',
					iconCls : 'btn-save',
					handler : this.save.createCallback(this.formPanel, this)
				}, {
					text : '重置',
					iconCls : 'btn-reset',
					handler : this.reset.createCallback(this.formPanel)
				}, {
					text : '取消',
					iconCls : 'btn-cancel',
					handler : this.cancel.createCallback(this)
				}];
	},//end of the initcomponents
	/**
	 * 初始化数据
	 * 
	 * @param {}
	 *            formPanel
	 */
	initData : function() {
		//加载表单对应的数据	
		if (this.id != null && this.id != 'undefined') {
			this.formPanel.getForm().load({
				deferredRender : false,
				url : __ctxPath + '/produce/getMaterialOutStock.do?id=' + this.id,
				waitMsg : '正在载入数据...',
				success : function(form, action) {
					var jsonData = Ext.util.JSON.decode(action.response.responseText);
					var res = jsonData.data;
					if(!Ext.isEmpty(res.project)){
						Ext.getCmp("materialOutStock.project.id").setValue(res.project.id);
						Ext.getCmp("materialOutStock.project.id").originalValue=res.project.id;
						Ext.getCmp("materialOutStockProName").setValue(res.project.proName);
						Ext.getCmp("materialOutStockProName").originalValue=res.project.proName;
					}
					if(!Ext.isEmpty(res.material)){
						Ext.getCmp("materialOutStock.material.id").setValue(res.material.id);
						Ext.getCmp("materialOutStock.material.id").originalValue=res.material.id;
						Ext.getCmp("materialOutStockMaterialName").setValue(res.material.materialName);
						Ext.getCmp("materialOutStockMaterialName").originalValue=res.material.materialName;
					}
				},
				failure : function(form, action) {
				}
			});
		}
	},
	/**
	 * 重置
	 * 
	 * @param {}
	 *            formPanel
	 */
	reset : function(formPanel) {
		formPanel.getForm().reset();
	},
	/**
	 * 取消
	 * 
	 * @param {}
	 *            window
	 */
	cancel : function(window) {
		window.close();
	},
	/**
	 * 保存记录
	 */
	save : function(formPanel, window) {
		if (formPanel.getForm().isValid()) {
			formPanel.getForm().submit({
				method : 'POST',
				waitMsg : '正在提交数据...',
				success : function(fp, action) {
					Ext.ux.Toast.msg('操作信息', '成功保存信息！');
					var gridPanel = Ext.getCmp('MaterialOutStockGrid');
					if (gridPanel != null) {
						gridPanel.getStore().reload();
					}
					window.close();
				},
				failure : function(fp, action) {
					Ext.MessageBox.show({
						title : '操作信息',
						msg : '信息保存出错，请联系管理员！',
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.ERROR
					});
//					window.close();
				}
			});
		}
	}//end of save

});